<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 产品管理
 * @author zxj
 *
 */
class Product_new extends MY_Controller
{
    private $t_product = 'mt_product';  //产品表
    function __construct()
    {
        parent::__construct();
    }


    /**
     * 查看详情
     * @param string $id
     * @return mixture
     */
    function inDetails($id = '')
    {
        $id = empty($id) ? gp('id') : $id;
        //产品信息
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        if ($productFirst['brandCustomerNum']) {
            $brandCustomer = $this->common_model->selectFirst('mt_customers', array('id' => $productFirst['brandCustomerNum']));
            $data['brandCustomer'] = $brandCustomer;
        }
        $categoryFirst = $this->common_model->selectFirst('mt_product_category', array('id' => $productFirst['category_id']));
        $statusFirst = $this->common_model->selectFirst('mt_product_status', array('id' => $productFirst['status_id']));
        $productFirst['category_name'] = $categoryFirst['name_'.$this->curLang];
        $productFirst['status_name'] = $statusFirst['name_'.$this->curLang];
        $productFirst['add_time'] = date('Y-m-d H:i:s',$productFirst['add_time']);
        if(isset($productFirst['isUnpackPass'])){
            $productFirst['isUnpackPass'] = $productFirst['isUnpackPass']?'Yes':'No';
        }
        if(isset($productFirst['isQcPass'])){
            $productFirst['isQcPass'] = $productFirst['isQcPass']?'Yes':'No';
        }
        if(isset($productFirst['acceptQuotation'])){
            $productFirst['acceptQuotation'] = $productFirst['acceptQuotation']?'Yes':'No';
        }
        if(isset($productFirst['supplementAcceptQuotation'])){
            $productFirst['supplementAcceptQuotation'] = $productFirst['supplementAcceptQuotation']?'Yes':'No';
        }
        if(isset($productFirst['needToConfirm'])){
            $productFirst['needToConfirm'] = $productFirst['needToConfirm']?'Yes':'No';
        }
        if(isset($productFirst['inWarranty'])){
            $productFirst['inWarrantyName'] = $productFirst['inWarranty']?'Yes':'No';
        }
        //获取订单信息
        $orderFirst = $this->common_model->selectFirst('mt_order', array('id' => $productFirst['oid']));
        $isPay = $orderFirst['is_pay'];
        $data['orderFirst'] = $orderFirst;
        //产品成色
        if($productFirst['physicalAppearance']){
            $damageFirst = $this->common_model->selectFirst('mt_product_damage',array('id'=>$productFirst['physicalAppearance']));
            $productFirst['damageName'] = $damageFirst['name_'.$this->curLang];
        }
        //#随机附件
        if ($productFirst['accessories']) {
            $accessoriesList = $this->common_model->selectCustom('mt_product_accessory', '', 'id desc', array('id' => explode(',', $productFirst['accessories'])));
            foreach ($accessoriesList as $rs) {
                $productFirst['accessoriesStr'] .= $rs['name_'.$this->curLang] . ';';
            }
        }
        //#故障
        if ($productFirst['fault']) {
            $faultList = $this->common_model->selectCustom('mt_product_fault', '', 'id desc', array('id' => explode(',', $productFirst['fault'])));
            foreach ($faultList as $rs) {
                $productFirst['faultStr'] .= $rs['name_'.$this->curLang] . ';';
            }
        }
        //#维修项
        if ($productFirst['repairItem']) {
            $ids = explode(',', $productFirst['repairItem']);
            if($productFirst['supplementRepairItem']){
                $supplementIds = explode(',',$productFirst['supplementRepairItem']);
                $ids = array_merge($ids,$supplementIds);
            }
            $repairItemList = $this->common_model->selectCustom('mt_product_repair_item', '', 'grade desc', array('id' => $ids));
            foreach ($repairItemList as &$rs) {
                //#补充维修项
                $rs['name'] = $rs['name_'.$this->curLang];
                if($supplementIds && in_array($rs['id'],$supplementIds)){
                    $rs['name'] .= '&nbsp;<span style="color:red;">+</span>';
                }
                //维修项价格
                $where = array('site_id' => $orderFirst['service_center'], 'labor_id' => $rs['id']);
                if ($brandCustomer['diyPrice']) {
                    $priceTable = 'mt_customer_price';
                    $where['customer_id'] = $productFirst['brandCustomerNum'];
                } else {
                    $priceTable = 'mt_base_price';
                }
                //收费对象
                $priceType = 'individual_price';
                if($productFirst['inWarranty']){
                    $priceType = 'brand_price';
                }elseif($orderFirst['merchant_num']){
                    $priceType = 'merchant_price';
                }
                $laborPrice = $this->common_model->selectFirst($priceTable, $where);
                $rs['price'] = $brandCustomer['diyPrice'] ? $laborPrice['price'] : $laborPrice[$priceType];
                $rs['price'] .= ' ' . $orderFirst['currency_code'];
            }
            unset($rs);
            $data['repairItemList'] = $repairItemList;
        }
        //#维修轨迹
        $trackList = $this->common_model->selectCustom('mt_product_track', array('pid' => $id));
        foreach ($trackList as &$rs) {
            $status = $this->common_model->selectFirst('mt_product_status', array('id' => $rs['end_sid']));
            $rs['status_name'] = $status['name_'.$this->curLang];
            $rs['is_sync'] = $rs['is_sync'] ? 'Yes' : 'No';
            $rs['add_time'] = date('Y-m-d H:i:s', $rs['add_time']);
        }
        unset($rs);
        $data['trackList'] = $trackList;
        //#配件
        $partsList = $this->common_model->selectCustom('mt_product_parts', array('pid' => $id));
        foreach ($partsList as &$rs) {
            //#补充配件
            if($rs['isSupplement']){
                $rs['name'] = $rs['name'].'&nbsp;<span style="color:red;">+</span>';
            }
            if ($rs['status'] == 2) {
                $rs['statusName'] = 'pending';
            } elseif ($rs['status'] == 3) {
                $rs['statusName'] = 'wait purchase';
            } else {
                $rs['statusName'] = 'Normal';
            }
            $rs['price'] = $rs['price'] . ' ' . $rs['currency'];
            $rs['rate_price'] = $rs['rate_price'] . ' ' . $rs['rate_currency'];
        }
        $data['partsList'] = $partsList;
        //#产品信息
        $data['productFirst'] = $productFirst;
        if($this->debug){
            gsh_output($data);
        }
        //#视图
        $data['curLang'] = $this->curLang;
        $this->common_model->flowBread($id,$productFirst['status_id'],$productFirst['oid']);
        $userRole = $this->users['role'];
        if($isPay && ($userRole=='admin' || $userRole=='repairer')) {
            $this->breadcrumb->append_crumb(
                array('url' => 'products/Product_new/repair/' . $id, 'name' => lang('product_bcbj'))
            );
        }
        $this->load->view('defaults/product_inDetail_new', $data);
        $this->load->view('defaults/common/footer');
    }


    /**
     * 拆包质检
     * @param string $id
     */
    function unpackQc($id = '')
    {
        //表单处理
        $data['post_type'] = __FUNCTION__;
        //接收数据
        $id = intval($id) ? intval($id) : gp('id');
        $data['id'] = $id;
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        if (!$productFirst) {
            show_error('This Product not found');
        }
        $orderFirst = $this->common_model->selectFirst('mt_order', array('id' => $productFirst['oid']));
        if (!$orderFirst) {
            $this->common_model->delete($this->t_product, array('oid' => $productFirst['oid']));
            show_error('This Order not found');
        }
        //判断是否修改数据
        $formData = gp();
        if ($formData['submits']) {
            $formData['accessories'] = implode(',', $formData['accessories']);
            $formData['fault'] = implode(',', $formData['fault']);
            $inData = $startData = array();
            foreach ($formData as $key => $val) {
                if (array_key_exists($key, $productFirst) && $productFirst[$key] != $val) {
                    $inData[$key] = $val;
                    $startData[$key] = $productFirst[$key];
                }
            }
            if ($inData) {
                $this->common_model->update($this->t_product, $inData, array('id' => $id));
                $this->common_model->productLog($id, $startData, $inData);
                //#更新状态
                if ($status_id == 1 || $status_id == 15) {
                    $this->common_model->resetStatus($id);
                }
            }
            //调试信息
            if(!$this->debug){
                redirect('products/Product_new/inDetails/' . $id);
            }
        } else {
            $serviceType = $this->common_model->selectFirst('mt_service_type', array('id' => $orderFirst['service_type']));
            $productFirst['serviceTypeName'] = $serviceType['name_'.$this->curLang];
            $statusFirst = $this->common_model->selectFirst('mt_product_status', array('id' => $status_id));
            $productFirst['statusName'] = $statusFirst['name_'.$this->curLang];
            if($productFirst['brandCustomerNum']) {
                $brandCustomer = $this->common_model->selectFirst('mt_customers', array('id' => $productFirst['brandCustomerNum']));
                $data['brandCustomer'] = $brandCustomer;
            }
            $categoryFirst = $this->common_model->selectFirst('mt_product_category', array('id' => $productFirst['category_id']));
            $productFirst['category_name'] = $categoryFirst['name_'.$this->curLang];
            $data['productFirst'] = $productFirst;
            $data['orderFirst'] = $orderFirst;
            //成色
            $damageList = $this->common_model->selectCustom('mt_product_damage');
            $data['damageList'] = $damageList;
            //随机附件
            $accessoriesList = $this->common_model->selectCustom('mt_product_accessory', array('category' => $productFirst['category_id']));
            $data['accessoriesList'] = $accessoriesList;
            //故障
            $faultList = $this->common_model->selectCustom('mt_product_fault', array('category' => $productFirst['category_id']));
            $data['faultList'] = $faultList;
            //default
            $ckdAvailable[$productFirst['available']] = 'checked';
            $data['ckdAvailable'] = $ckdAvailable;
            $ckdIsUnpackPass[$productFirst['isUnpackPass']] = 'checked';
            $data['ckdIsUnpackPass'] = $ckdIsUnpackPass;
            $ckdDesCst[$productFirst['descriptionConsistent']] = 'checked';
            $data['ckdDesCst'] = $ckdDesCst;
            $ckdDamage[$productFirst['physicalAppearance']] = 'checked';
            $data['ckdDamage'] = $ckdDamage;
            $ckdInWarranty[$productFirst['inWarranty']] = 'checked';
            $data['ckdInWarranty'] = $ckdInWarranty;
            //附件
            $product_Accessories = explode(',', $productFirst['accessories']);
            foreach ($product_Accessories as $val) {
                $ckdAccessories[$val] = 'checked';
                $data['ckdAccessories'] = $ckdAccessories;
            }
            //故障
            $product_Fault = explode(',', $productFirst['fault']);
            foreach ($product_Fault as $val) {
                $ckdFault[$val] = 'checked';
                $data['ckdFault'] = $ckdFault;
            }
            //调试信息
            if($this->debug){
                gsh_output($data);
            }
            //views
            $data['curLang'] = $this->curLang;
            $this->common_model->flowBread($id,$productFirst['status_id'],$productFirst['oid']);
            $this->load->view('defaults/product_unpackQc_new', $data);
            $this->load->view('defaults/common/footer');
        }
    }


    /**
     * 接单
     * @param string $id
     */
    function joinProduct($id = '')
    {
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        //#更新状态
        if ($status_id == 2) {
            $this->common_model->resetStatus($id);
        }
        //调试信息
        if(!$this->debug){
            redirect('products/Product_new/repair/' . $id);
        }
    }


    /**
     * 维修报价
     * @param string $id
     */
    function repair($id = '')
    {
        //表单处理
        $data['post_type'] = __FUNCTION__;
        //接收数据
        $id = intval($id) ? intval($id) : gp('id');
        $data['id'] = $id;
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        if (!$productFirst) {
            show_error('This Product not found');
        }
        $orderFirst = $this->common_model->selectFirst('mt_order', array('id' => $productFirst['oid']));
        $isPay = $orderFirst['is_pay'];
        if (!$orderFirst) {
            $this->common_model->delete($this->t_product, array('oid' => $productFirst['oid']));
            show_error('此维修单不存在,将删除此产品数据');
        }
        //判断是否修改数据
        $formData = gp();
        if ($formData['submits']) {
            $repairItem = implode(',', $formData['repairItem']);
            //补充报价
            if ($isPay) {
                $formData['supplementRepairItem'] = $repairItem;
                unset($formData['repairItem']);
            } else {
                $formData['repairItem'] = $repairItem;
            }
            $inData = array();
            foreach ($formData as $key => $val) {
                if (array_key_exists($key, $productFirst) && $productFirst[$key] != $val) {
                    $inData[$key] = $val;
                }
            }
            if ($inData) {
                $this->common_model->update($this->t_product, $inData, array('id' => $id));
                $this->common_model->setProductPrice($id);
                //#更新状态
                if ($status_id == 3) {
                    $this->common_model->resetStatus($id);
                }
            }
            //调试信息
            if(!$this->debug){
                redirect('products/Product_new/inDetails/' . $id);
            }
        } else {
            $serviceType = $this->common_model->selectFirst('mt_service_type', array('id' => $orderFirst['service_type']));
            $productFirst['serviceTypeName'] = $serviceType['name_'.$this->curLang];
            $statusFirst = $this->common_model->selectFirst('mt_product_status', array('id' => $status_id));
            $productFirst['statusName'] = $statusFirst['name_'.$this->curLang];
            $brandCustomer = $this->common_model->selectFirst('mt_customers', array('id' => $productFirst['brandCustomerNum']));
            $data['brandCustomer'] = $brandCustomer;
            $categoryFirst = $this->common_model->selectFirst('mt_product_category', array('id' => $productFirst['category_id']));
            $productFirst['category_name'] = $categoryFirst['name_'.$this->curLang];
            $data['productFirst'] = $productFirst;
            $data['orderFirst'] = $orderFirst;
            //维修项
            $repairItemList = $this->common_model->selectCustom('mt_product_repair_item', array('category' => $productFirst['category_id']));
            $data['repairItemList'] = $repairItemList;
            //default
            $ckdAvailable[$productFirst['available']] = 'checked';
            $data['ckdAvailable'] = $ckdAvailable;
            $ckdInWarranty[$productFirst['inWarranty']] = 'checked';
            $data['ckdInWarranty'] = $ckdInWarranty;
            //#维修项
            $productFirst['repairItem'] = $isPay ? $productFirst['supplementRepairItem'] : $productFirst['repairItem'];
            if ($productFirst['repairItem']) {
                $product_repairItem = explode(',', $productFirst['repairItem']);
                foreach ($product_repairItem as $val) {
                    $ckdRepairItem[$val] = 'checked';
                    $data['ckdRepairItem'] = $ckdRepairItem;
                }
            }
            //views
            $data['curLang'] = $this->curLang;
            $this->common_model->flowBread($id,$productFirst['status_id'],$productFirst['oid']);
            $this->load->view('defaults/product_repair_new', $data);
            $this->load->view('defaults/common/footer');
        }
    }


    /**
     * 开始维修
     * @param string $id
     */
    function startRepair($id = ''){
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        //#更新状态
        if ($status_id == 6) {
            $this->common_model->resetStatus($id);
        }
        //调试信息
        if(!$this->debug){
            redirect('products/Product_new/inDetails/' . $id);
        }
    }



    /**
     * 装配完成待QC
     * @param string $id
     */
    function assembledQc($id = ''){
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        //#更新状态
        if ($status_id == 7) {
            $this->common_model->resetStatus($id);
        }
        //调试信息
        if(!$this->debug){
            redirect('products/Product_new/inDetails/' . $id);
        }
    }


    /**
     * 维修完成
     * @param string $id
     */
    function endRepair($id = ''){
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        //#更新状态
        if ($status_id == 9) {
            $this->common_model->resetStatus($id);
        }
        //调试信息
        if(!$this->debug){
            redirect('products/Product_new/inDetails/' . $id);
        }
    }



    /**
     * 入库完成
     * @param string $id
     */
    function inStock($id = ''){
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        //#更新状态
        if ($status_id == 12) {
            $this->common_model->resetStatus($id);
        }
        //调试信息
        if(!$this->debug){
            redirect('products/Product_new/inDetails/' . $id);
        }
    }



    /**
     * 接单质检
     * @param string $id
     */
    function joinQc($id = ''){
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        //#更新状态
        $qcStatusArr = array(10,15,16);
        if(in_array($status_id,$qcStatusArr)){
            $this->common_model->resetStatus($id);
        }
        //调试信息
        if(!$this->debug){
            redirect('products/Product_new/inDetails/' . $id);
        }
    }



    /**
     * 质检
     * @param string $id
     */
    function serviceQc($id = '')
    {
        //表单处理
        $data['post_type'] = __FUNCTION__;
        //接收数据
        $id = intval($id) ? intval($id) : gp('id');
        $data['id'] = $id;
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        if (!$productFirst) {
            show_error('This Product not found');
        }
        $orderFirst = $this->common_model->selectFirst('mt_order', array('id' => $productFirst['oid']));
        if (!$orderFirst) {
            $this->common_model->delete($this->t_product, array('oid' => $productFirst['oid']));
            show_error('此维修单不存在,将删除此产品数据');
        }
        //判断是否修改数据
        $formData = gp();
        if ($formData['submits']) {
            $formData['accessories'] = implode(',', $formData['accessories']);
            $formData['fault'] = implode(',', $formData['fault']);
            $inData = $startData = array();
            foreach ($formData as $key => $val) {
                if (array_key_exists($key, $productFirst) && $productFirst[$key] != $val) {
                    $inData[$key] = $val;
                    $startData[$key] = $productFirst[$key];
                }
            }
            if ($inData) {
                $this->common_model->update($this->t_product, $inData, array('id' => $id));
                $this->common_model->productLog($id, $startData, $inData);
                //#更新状态
                if ($status_id == 11) {
                    $this->common_model->resetStatus($id);
                }
            }
            //调试信息
            if(!$this->debug){
                redirect('products/Product_new/inDetails/' . $id);
            }
        } else {
            $serviceType = $this->common_model->selectFirst('mt_service_type', array('id' => $orderFirst['service_type']));
            $productFirst['serviceTypeName'] = $serviceType['name_'.$this->curLang];
            $statusFirst = $this->common_model->selectFirst('mt_product_status', array('id' => $status_id));
            $productFirst['statusName'] = $statusFirst['name_'.$this->curLang];
            if($productFirst['brandCustomerNum']) {
                $brandCustomer = $this->common_model->selectFirst('mt_customers', array('id' => $productFirst['brandCustomerNum']));
                $data['brandCustomer'] = $brandCustomer;
            }
            $categoryFirst = $this->common_model->selectFirst('mt_product_category', array('id' => $productFirst['category_id']));
            $productFirst['category_name'] = $categoryFirst['name_'.$this->curLang];
            $data['productFirst'] = $productFirst;
            $data['orderFirst'] = $orderFirst;
            //成色
            $damageList = $this->common_model->selectCustom('mt_product_damage');
            $data['damageList'] = $damageList;
            //随机附件
            $accessoriesList = $this->common_model->selectCustom('mt_product_accessory', array('category' => $productFirst['category_id']));
            $data['accessoriesList'] = $accessoriesList;
            //故障
            $faultList = $this->common_model->selectCustom('mt_product_fault', array('category' => $productFirst['category_id']));
            $data['faultList'] = $faultList;
            //default
            $ckdAvailable[$productFirst['available']] = 'checked';
            $data['ckdAvailable'] = $ckdAvailable;
            $ckdIsQcPass[$productFirst['isQcPass']] = 'checked';
            $data['ckdIsQcPass'] = $ckdIsQcPass;
            $ckdDesCst[$productFirst['descriptionConsistent']] = 'checked';
            $data['ckdDesCst'] = $ckdDesCst;
            $ckdDamage[$productFirst['physicalAppearance']] = 'checked';
            $data['ckdDamage'] = $ckdDamage;
            $ckdInWarranty[$productFirst['inWarranty']] = 'checked';
            $data['ckdInWarranty'] = $ckdInWarranty;
            //附件
            $product_Accessories = explode(',', $productFirst['accessories']);
            foreach ($product_Accessories as $val) {
                $ckdAccessories[$val] = 'checked';
                $data['ckdAccessories'] = $ckdAccessories;
            }
            //故障
            $product_Fault = explode(',', $productFirst['fault']);
            foreach ($product_Fault as $val) {
                $ckdFault[$val] = 'checked';
                $data['ckdFault'] = $ckdFault;
            }
            //调试信息
            if($this->debug){
                gsh_output($data);
            }
            //views
            $data['curLang'] = $this->curLang;
            $this->common_model->flowBread($id,$productFirst['status_id'],$productFirst['oid']);
            $this->load->view('defaults/product_serviceQc_new', $data);
            $this->load->view('defaults/common/footer');
        }
    }


    /**
     * 删除
     * @param string $id
     * @param string $page
     */
    function del($id = '', $page = '')
    {
        $id = intval($id);
        $where = array('id' => $id);
        $this->common_model->delete($this->t_product, $where);
        redirect(blang_url('product/Product_new/index/?page=' . intval($page)));
    }



    /**
     * 刷新产品价格
     * @param string $id
     * @return bool
     */
    function reloadPrice($id = ''){
        $id = intval($id);
        $this->common_model->setProductPrice($id);
        if(!$this->debug){
            redirect('products/Product_new/inDetails/' . $id);
        }
        return true;
    }





}
